﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
using Winista;
using Winista.Text.HtmlParser;
using Winista.Text.HtmlParser.Lex;
using Winista.Text.HtmlParser.Util;
using Winista.Text.HtmlParser.Nodes;

namespace DwonloadTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "https://www.ysts8.net/Yshtml/Ys28770.html";
             url = "https://www.ysts8.net/Yshtml/Ys17930.html";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream ResponseStream = response.GetResponseStream();
            StreamReader StreamReader = new StreamReader(ResponseStream, Encoding.GetEncoding("Gb2312"));
            string re = StreamReader.ReadToEnd();

            Lexer lexer = new Lexer(re);
            Parser parser = new Parser(lexer);
            NodeList htmlNodes = parser.Parse(null);
            INode node = null;
            for (int i = 0; i < htmlNodes.Count; i++)
            {
                node = Search(htmlNodes[i]);
            }
            List<string> names = new List<string>();
            if (node != null)
            {
                for (int i = 0; i < node.Children.Count; i++)
                {
                    INode ulNode = node.Children[i];
                    if (ulNode is TextNode)
                    {
                        TextNode textNode = ulNode as TextNode;
                        ITag tag = textNode.NextSibling as ITag;
                        if (tag == null)
                        {
                            continue;
                        }
                        string name = tag.TagName;
                        if (name.ToLower() == "ul")
                        {
                            for (int j = 0; j < tag.Children.Count; j++)
                            {
                                INode liNode = tag.Children[j];
                                if (liNode.Children != null)
                                {
                                    for (int k = 0; k < liNode.Children.Count; k++)
                                    {
                                        INode aNode = liNode.Children[k];
                                        object mpName = (aNode as ITag).Attributes["TITLE"];
                                        if (mpName != null)
                                        {
                                            string mpNameStr = mpName.ToString();
                                            names.Add(mpNameStr);
                                        }
                                    }
                                }

                            }
                        }
                    }
                }
                Dwonload(names);
                StreamReader.Close();
                ResponseStream.Close();
            }
        }

        static INode Search(INode node)
        {
            if (node is ITag)
            {
                ITag tag = node as ITag;
                object value = tag.Attributes["CLASS"];
                string name = tag.TagName;
                if (name.ToLower() == "div")
                {
                    if (value != null && value.ToString().ToLower() == "ny_l")
                    {
                        return node;
                    }
                }
            }
            if (node.Children != null && node.Children.Count > 0)
            {
                for (int i = 0; i < node.Children.Count; i++)
                {
                    INode search = Search(node.Children[i]);
                    if (search != null)
                    {
                        return search;
                    }
                }
            }
            return null;
        }

        static void Dwonload(List<string> names)
        {
            string path = @"D:\resource\最后一个道士\下";
            for (int i = 0; i < names.Count; i++)
            {

                string url = String.Format("http://177k.tt56w.com:8000/恐怖小说/最后一个道士(下)/{0}?1131727451783x1587362695x1131733582443-af024d82a0b49763f746271a4781a4fd3+ ", names[i]);
                string file = String.Format("{0}\\{1}", path, names[i]);
                if (!File.Exists(file))
                {
                    System.Console.WriteLine("文件" + names[i] + "正在下载");
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    request.Method = "GET";
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream stream = response.GetResponseStream();
                    FileStream fileStream = new FileStream(file, FileMode.Create);
                    byte[] buffer = new byte[1024];
                    int count = stream.Read(buffer, 0, 1024);
                    while (count > 0)
                    {
                        fileStream.Write(buffer, 0, count);
                        buffer = new byte[1024];
                        count = stream.Read(buffer, 0, 1024);
                    }
                    stream.Close();
                    fileStream.Close();
                    System.Console.WriteLine("文件" + names[i] + "下载成功");
                }
                else
                {
                    System.Console.WriteLine("文件" + names[i] + "已经存在");
                }
            }
        }
    }
};